package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.pojo.OrderSettingDto;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author spt
 * @description
 * @date 2019/9/29
 **/
@Service
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;


    @Override
    public List<OrderSettingDto> getOrderSettingByMonth(Integer year, Integer month) {
        //1. 从数据库查基本信息
        List<OrderSetting> orderSettings = orderSettingDao.getOrderSettingByMonth(year, month);

        //2. 新建前端需要的对象列表并封装
        List<OrderSettingDto> orderSettingDtos = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettings) {
            OrderSettingDto orderSettingDto = new OrderSettingDto();
            orderSettingDto.setDate(orderSetting.getOrderDate().getDate());
            orderSettingDto.setNumber(orderSetting.getNumber());
            orderSettingDto.setReservations(orderSetting.getReservations());
            orderSettingDtos.add(orderSettingDto);
        }

        return orderSettingDtos;
    }

    @Override
    public boolean add(OrderSetting orderSetting) {
        System.out.println(orderSetting.getOrderDate());
        OrderSetting byDate = orderSettingDao.findByDate(orderSetting.getOrderDate());

        if (byDate != null) {
            orderSettingDao.updateNumberByDate(orderSetting.getNumber(),orderSetting.getOrderDate());
            return true;
        }

        return orderSettingDao.add(orderSetting);
    }

    @Override
    public void addBatch(List<ArrayList<String>> sheetData) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        for (int i = 0; i < sheetData.size(); i++) {
            ArrayList<String> data = sheetData.get(i);
            OrderSetting orderSetting = new OrderSetting();
            try {
                orderSetting.setOrderDate(sdf.parse(data.get(0)));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            orderSetting.setNumber(Integer.parseInt(data.get(1)));

            this.add(orderSetting);
        }
    }

    /* 修改预约数量 */
    @Override
    public boolean updateNumberByDate(int number, Date orderDate) {
        return orderSettingDao.updateNumberByDate(number, orderDate) > 0;
    }

    /* 通过指定时间返回OrderSetting */
    @Override
    public OrderSetting findByDate(Date orderDate) {
        return orderSettingDao.findByDate(orderDate);
    }

    /* 通过指定时间返回剩余预约数 */
    @Override
    public Integer findRereservationsByDate(Date date) {
        Integer reserve = orderSettingDao.findRereservationsByDate(date);
        Integer number = orderSettingDao.findNumberByDate(date);
        return number - reserve;
    }
}
